<template>
    <div>
        <basic-layout title="电子餐券" :showFilterBtn="true" @handleAdd="handleAdd"
            @handleFilter="$refs.queryPopupRef.show = true">
            <div class="columnFlex">
                <van-row class="listCon">
                    <van-pull-refresh class="pullRefresh" v-model="refreshLoading" @refresh="onRefresh">
                        <van-list v-model="loading" :finished="finished" :finished-text="listData.length ? '没有更多了' : ''"
                            @load="onLoad">
                            <van-swipe-cell v-for="(val, ind) in listData" :key="ind">
                                <div class="cellContent">
                                    <div class="top" @click="handleDetail(val)">
                                        <p class="tit">
                                            <b>
                                                {{ val.canteenName || '-' }}
                                            </b>
                                            <a class="status status1" v-if=" val.mealticketApplyStatus==='1'">{{
                                                val.mealticketApplyStatusName}}</a>
                                            <a class="status status2" v-else-if=" val.mealticketApplyStatus==='2'">{{
                                                val.mealticketApplyStatusName}}</a>
                                            <a class="status status3" v-else-if=" val.mealticketApplyStatus==='5'&& val.approvalResult==='1'">{{
                                                val.mealticketApplyStatusName}}</a>
                                            <a class="status status4" v-else-if=" val.mealticketApplyStatus==='5'&& val.approvalResult==='2'">{{
                                                val.mealticketApplyStatusName}}</a>
                                            <a class="status" v-else>{{
                                                val.mealticketApplyStatusName}}</a>
                                        </p>
                                    </div>
                                    <div class="bottom">
                                        <div class="left" @click="handleDetail(val)">
                                            <div>
                                                <p v-if="val.mealticketEffectiveType==='6'">
                                                    <span
                                                        v-if="val.mealticketEffectiveType!=='6'">{{val.mealticketEffectiveDate}}</span>
                                                    <span class="label">餐券有效日期：</span>
                                                    无期限
                                                </p>
                                                <p v-if="val.mealticketEffectiveType!=='6'">
                                                    <span class="label">餐券有效日期：</span>
                                                    {{val.mealticketEffectiveDate}}
                                                </p>
                                                <p>
                                                    <span class="label">餐次：</span>
                                                    {{ val.mealsTypeName }}
                                                </p>
                                                <p>
                                                    <span class="label">餐券数量：</span>
                                                    <span>{{ val.mealticketApplyNum }}</span>
                                                </p>
                                                <p v-if="val.mealticketApplyStatus==='5'&& val.approvalResult==='1'">
                                                    <span class="label">待使用数量：</span>
                                                    <span>{{ val.beUsedNum }}</span>
                                                </p>
                                            </div>
                                        </div>
                                        <div class="right" v-if="val.mealticketApplyStatus==='5'&& val.approvalResult==='1'">
                                            <img src="images/convaccess/qrcode.svg"
                                                @click="$refs.codeViewRef.getData(val.mealticketId);">
                                        </div>
                                    </div>
                                </div>
                            </van-swipe-cell>
                        </van-list>
                        <van-empty v-if="!loading && !listData.length" class="custom-image"
                            :image="require('../../../../assets/images/empty.png')" description="暂无数据" />
                    </van-pull-refresh>
                </van-row>
            </div>
        </basic-layout>
        <!-- 筛选条件弹出框 -->
        <queryPopup ref="queryPopupRef" @handleSearch="onRefresh" @handleReset="handleReset">
            <van-form class="filterBox">
                <label>食堂列表</label>
                <van-field>
                    <template #input>
                        <div class="textCon" @click="showPicker = true">
                            <span v-if="!canteenText" style="color: #aaa">请选择食堂列表</span>
                            <span v-else>{{ canteenText }}</span>
                        </div>
                    </template>
                </van-field>
                <label>餐券日期</label>
                <van-field>
                    <template #input>
                        <div class="textCon" @click="showStatusDatePicker = true">
                            <span v-if="!mealticketApplyDateText" style="color: #aaa">请选择餐券日期</span>
                            <span v-else>{{ mealticketApplyDateText }}</span>
                        </div>
                    </template>
                    <!-- <template #button>
                        <van-icon v-if="statusText" name="cross" @click="clearPicker()" />
                    </template> -->
                </van-field>
                <label>申请状态</label>
                <van-field>
                    <template #input>
                        <div class="textCon" @click="showStatusPicker = true">
                            <span v-if="!statusText" style="color: #aaa">请选择申请状态</span>
                            <span v-else>{{ statusText }}</span>
                        </div>
                    </template>
                    <template #button>
                        <van-icon v-if="statusText" name="cross" @click="clearPicker()" />
                    </template>
                </van-field>
            </van-form>
        </queryPopup>
        <!-- 预定日期 -->
        <van-calendar v-model="showStatusDatePicker" @confirm="onTimeConfirm" />
        <!-- 食堂列表选择 -->
        <van-action-sheet v-model="showPicker" cancel-text="取消" :actions="canteenList" @select="onConfirm" />
        <!-- 预定状态选择 -->
        <van-action-sheet v-model="showStatusPicker" cancel-text="取消" :actions="mealticketApplyStatusList"
            @select="onStatusConfirm" />
        <!-- 新增/详情/编辑 弹出框 -->
        <fullscreenModal :modal="fullscreenModal" :isAdd="isAdd" label="餐券申请" :isShowEdit="isShowEdit"
            :editType="editType" @handleClose="fullscreenModal = false" @handleEdit="handleEdit"
             @handleCancel="editType = 'detail'" @handleSave="$refs.editFormRef.handleSave()">
            <!-- <detail v-show="editType == 'detail'" ref="detailRef"></detail> -->
            <detail-info v-show="editType == 'detail'" ref="detailRef" />
            <edit-form v-show="editType == 'edit'" @close="fullscreenModal = false" @refresh="onRefresh"
                ref="editFormRef" />
        </fullscreenModal>
        <full-loading v-if="fullLoading"></full-loading>
        <!-- 明细 -->
        <codeView ref="codeViewRef" title="电子餐券" />
    </div>
</template>

<script>
    import { queryDic } from '@/api/common';
    import { getCanteenInfoByLoginUser } from '@/api/srvAssurance/smarcatering/oneWeekMenu';
    import { getPage, add, edit, deleteById, getDetailInfoById, getInfoById, editStatus, getDetailAllInfoById } from '@/api/srvAssurance/smarcatering/electronMealticket';
    import editForm from "./editForm.vue";
    import codeView from "./codeView.vue";
    import detailInfo from "./detailInfo.vue";
    export default {
        components: {
            editForm,
            codeView,
            detailInfo,
        },
        data() {
            return {
                isShowEdit: false,
                showStatusDatePicker: false,
                loading: false,
                fullLoading: false,
                refreshLoading: false,
                fullscreenModal: false,
                showPicker: false,
                showStatusPicker: false,
                showCancelModal: false,
                finished: false,
                formData: {
                    canteenId: '',
                    mealticketApplyStatus: '',
                    pageNum: 1,
                    pageSize: 20,
                    mealticketApplyDate: ''
                },
                canteenText: "",
                statusText: "",
                mealticketApplyDateText: '',
                listData: [], // 列表数据
                canteenList: [], // 食堂列表
                mealticketApplyStatusList: [
                    // { value: 1, name: '待提交' },
                    { value: 2, name: '审批中' },
                    { value: 3, name: '审批通过' },
                    { value: 4, name: '审批不通过' },
                ],
                isAdd: false,
                editType: "",
                cancelFormData: {
                    bookeId: '', // 取消标识
                    cancelReason: '', // 取消原因
                },
                editParam: {},
            };
        },
        mounted() {
            this.queryDicData();
        },
        methods: {
            handleEdit() {
                getInfoById(this.editParam.mealticketId)
                    .then((res) => {
                        if (res.code === 1000 && res.data) {
                            this.editType = "edit";
                            this.$refs.editFormRef.resetForm();
                            // this.$refs.editFormRef.getData(this.currentParam);
                            this.$refs.editFormRef.getData({ data: res.data });
                            // this.fullscreenModal = true;

                        }
                    })
                    .catch(() => {

                    });
            },
            handleDetail(item) {
                this.fullscreenModal = true;
                this.editType = "detail";
                this.isAdd = false;
                // if (item.mealticketApplyStatus === '1') {
                //     this.isShowEdit = true;
                // } else {
                //     this.isShowEdit = false;
                // }
                this.isShowEdit = false;
                this.editParam = item;
                this.$refs.detailRef.getData(item);
            },
            sub(item) {
                this.$Dialog
                    .confirm({
                        title: "是否确定提交该数据？",
                    })
                    .then(async () => {
                        var res = await editStatus({ mealticketId: item.mealticketId });
                        if (res.code === 1000) {
                            this.$Notify({ type: "success", message: "提交成功！" });
                            this.onRefresh();
                        }
                    })
                    .catch(() => { });
            },
            upd(item) {
                this.$refs.editFormRef.resetForm();
                getInfoById(item.mealticketId)
                    .then((res) => {
                        if (res.code === 1000 && res.data) {
                            this.isAdd = true;
                            this.editType = "edit";
                            this.$refs.editFormRef.getData({ data: res.data });
                            this.fullscreenModal = true;

                        }
                    })
                    .catch(() => {

                    });

            },
            del(item) {
                this.$Dialog
                    .confirm({
                        title: "是否确定删除该数据？",
                    })
                    .then(async () => {
                        var res = await deleteById({ mealticketIds: [item.mealticketId] });
                        if (res.code === 1000) {
                            this.$Notify({ type: "success", message: "删除成功！" });
                            this.onRefresh();
                        }
                    })
                    .catch(() => { });
            },
            // 预定日期选择
            onTimeConfirm(time) {
                this.formData.mealticketApplyDate = new Date(time).format('yyyy-MM-dd');
                this.mealticketApplyDateText = new Date(time).format('yyyy-MM-dd');
                this.showStatusDatePicker = false;
                this.onRefresh();
            },
            // 获取表格数据
            getListData() {
                if (!this.formData.canteenId) {
                    this.finished = true;
                    this.loading = false;
                    return;
                }
                this.loading = true;
                getPage(this.formData).then((res) => {
                    this.loading = false;
                    if (res.code === 1000 && res.data) {
                        this.listData = [...this.listData, ...res.data.records];
                        if (this.listData.length >= res.data.total) {
                            this.finished = true;
                        }
                    }
                }).catch(() => {
                    this.loading = false;
                    this.finished = true;
                });
            },
            onRefresh() {
                this.refreshLoading = false;
                this.finished = false;
                this.listData = [];
                this.formData.pageNum = 1;
                this.getListData();
            },
            onLoad() {
                this.formData.pageNum++;
                this.getListData();
            },
            // 重置
            handleReset() {
                this.formData = {
                    canteenId: '',
                    mealticketApplyStatus: '',
                    pageNum: 1,
                    pageSize: 20,
                    mealticketApplyDate: ''
                };
                if (this.canteenList.length) {
                    this.formData.canteenId = this.canteenList[0].canteenId;
                    this.canteenText = this.canteenList[0].canteenName;
                }
                this.statusText = "";
                this.mealticketApplyDateText = "";
                this.onRefresh();
            },
            // 食堂列表选择
            onConfirm(item) {
                this.formData.canteenId = item.canteenId;
                this.canteenText = item.name;
                this.showPicker = false;
                this.onRefresh();
            },
            // 预定状态选择
            onStatusConfirm(item) {
                this.formData.mealticketApplyStatus = item.value;
                this.statusText = item.name;
                this.showStatusPicker = false;
                this.onRefresh();
            },
            // 清空查询条件
            clearPicker() {
                this.formData.mealticketApplyStatus = "";
                this.statusText = "";
                this.mealticketApplyDateText = "";
                this.onRefresh();
            },
            // 新建数据
            handleAdd() {
                this.fullscreenModal = true;
                this.editType = "edit";
                this.isAdd = true;
                this.$refs.editFormRef.resetForm();
                this.$refs.editFormRef.getDay();
            },

            // 下拉框数据
            queryDicData() {
                // 食堂列表
                getCanteenInfoByLoginUser().then(res => {
                    if (res.code === 1000 && res.data) {
                        res.data.forEach(item => {
                            item.name = item.canteenName;
                        })
                        this.canteenList = res.data;
                        if (this.canteenList.length) {
                            this.formData.canteenId = this.canteenList[0].canteenId;
                            this.canteenText = this.canteenList[0].canteenName;
                            this.onRefresh();
                        }
                        this.$nextTick(() => {
                            this.$refs.editFormRef.canteenList = this.canteenList;
                        })
                    }
                })
                queryDic('SA_MEALS_TYPE_CODE').then(res => {
                    if (res.code == 1000) {
                        res.data.forEach(item => {
                            item.name = item.valueAlias;
                            item.value = item.codeValue;
                        })
                        this.$nextTick(() => {
                            this.$refs.editFormRef.mealsTypeList = res.data;
                        })
                        // this.mealsTypeList = res.data
                    }
                });
            }
        },
    };
</script>

<style scoped lang="less">
    .listCon {
        .cellContent {
            line-height: 25px;
            padding: 0;
            position: relative;

            .top {
                padding: 5px 10px;
                box-sizing: border-box;
                position: relative;
                border-bottom: 1px solid #eee;

                .tit {
                    font-size: 15px;
                    height: 30px;

                    b span {
                        font-size: 14px;
                    }

                    .status {
                        background: rgb(70, 172, 240);
                        font-size: 9px;
                        color: #fff;
                        margin-left: 8px;
                        padding: 2px 5px;
                        border-radius: 3px;
                    }

                    .status2 {
                        background: #f1dc7f;
                    }

                    .status3 {
                        background: #1aa78f;
                    }

                    .status4 {
                        background: #b4afaf;
                    }
                }

                .type {
                    background: #f2a84e;
                    font-size: 9px;
                    color: #fff;
                    margin-left: 8px;
                    padding: 2px 3px;
                    border-radius: 3px;
                }
            }

            .bottom {
                padding: 5px 10px;
                box-sizing: border-box;
                display: flex;
                overflow: hidden;
                align-items: center;
                border-bottom: 1px solid #eee;

                .left {
                    flex: 1;
                    overflow: hidden;
                }

                .right {
                    margin: 0 5px 0 10px;

                    img {
                        width: 35px;
                        height: 35px;
                    }

                    p {
                        color: #aaa;
                        font-size: 13px;
                    }
                }
            }
        }

        .flexBtn {
            display: flex;
            justify-content: space-between;

            p {
                flex: 1;
                text-align: center;
                height: 35px;
                line-height: 35px;
                color: #4eb3e9;
                font-size: 15px;
            }

            p:nth-of-type(1) {
                box-sizing: border-box;
                border-right: 1px solid #eee;
                color: #f25454;
            }
        }
    }
</style>